package com.zry.traced.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.util.Log
import android.view.View
import com.zry.traced.R
import com.zry.traced.been.FlowCodeInfo
import com.zry.traced.databinding.ActivityComplementInBoxBinding
import com.zry.traced.presenter.HttpServer
import com.zry.traced.retrofit.ShowUserView
import com.zry.traced.ui.base.BaseActivity
import com.zry.traced.utils.InputCodeFilter
import com.zry.traced.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_complement_in_box.*
import kotlinx.android.synthetic.main.activity_section_number_storage.*
import kotlinx.android.synthetic.main.common_title.*

class ComplementInBoxActivity : BaseActivity(), ShowUserView {
    private var complementInBoxBinding: ActivityComplementInBoxBinding? = null
    private var flowCodeInfo: FlowCodeInfo? = null
    private var title = ""
    private var parentCode: String? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_complement_in_box
    }

    override fun initData() {
        title = intent.getStringExtra("title")

        titleCenterText.text = title
        commonLeftLinearLayout.setOnClickListener { finish() }

        when (title) {
            "拆码出箱" -> {
                submitInBoxBtn.setText(R.string.startDismantling)
            }
        }

        complementInBoxBinding = viewDataBinding as ActivityComplementInBoxBinding
        complementInBoxBinding?.handlerClick = this

        initView()
    }

    private fun initView() {
        inBoxScanCodeET.filters = arrayOf<InputFilter>(InputCodeFilter())
        inBoxScanCodeET.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit
            override fun afterTextChanged(editable: Editable?) {
                val codeString = inBoxScanCodeET.text.toString().trim()
                Log.e("codeString", codeString)
                if (!codeString.isEmpty()) {
                    setRequestFocusFalse()
                    getFlowCodeInfo(codeString)
                }
            }
        })
    }

    fun getFlowCodeInfo(codeString: String) {
        when (title) {
            "补码入箱" -> {
                HttpServer(this).getFlowCodeInfo(codeString, 1, this)
            }
            "拆码出箱" -> {
                HttpServer(this).getFlowCodeInfo(codeString, 2, this)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            21 -> {
                getEditFocusTrue()
                flowCodeInfo = data as FlowCodeInfo
                if (flowCodeInfo?.state == "1") {
                    inboxScanCodeTV.text = flowCodeInfo?.CodeID
                    parentCode = flowCodeInfo?.CodeID

                    complementInBoxBinding?.flowCodeInfo = flowCodeInfo
                    when (flowCodeInfo?.Level) {
                        "1" -> inBoxingLevel.text = "单品包装" //拆码没有单品包装
                        "2" -> inBoxingLevel.text = "二级码包装"
                        "3" -> inBoxingLevel.text = "三级码包装"
                    }
                }else{
                    toast(flowCodeInfo!!.Message)
                }
            }
        }
    }

    fun inBoxHandlerClick(view: View) {
        when (view.id) {
            R.id.submitInBoxBtn -> {
                if (inboxScanCodeTV.text.toString().isEmpty()) {
                    return toast("拆箱父级码不能为空")
                } else {
                    val intent = Intent(this, ComplementInBoxSubmitActivity::class.java)
                    intent.putExtra("title", title)
                    intent.putExtra("flowCodeInfo", flowCodeInfo)
                    intent.putExtra("parentCode", parentCode)
                    startActivity(intent)
                }
            }
        }
    }

    private fun getEditFocusTrue() {
        inBoxScanCodeET.setText("")
        //获取焦点
        inBoxScanCodeET.isFocusable = true
        inBoxScanCodeET.isFocusableInTouchMode = true
        //显示光标
        inBoxScanCodeET.requestFocus()
        inBoxScanCodeET.isCursorVisible = true
    }


    private fun setRequestFocusFalse() {
        //取消焦点
        inBoxScanCodeET.isFocusable = false
        inBoxScanCodeET.isFocusableInTouchMode = false
        //隐藏光标
        inBoxScanCodeET.isCursorVisible = false
    }


}
